package cn.itcast.travel.dao.impl;

import cn.itcast.travel.dao.OrderDao;
import cn.itcast.travel.domain.Order;
import cn.itcast.travel.domain.OrderDo;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class OrderDaoImp implements OrderDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public void addOrder(Order order) {
        String sql="insert into tab_order(rid,time,ispay,out_trade_no,uid) value(?,?,?,?,?);";
        jdbcTemplate.update(sql,order.getRoute().getRid(),order.getTime(),order.getIspay(),order.getOut_trade_no(),order.getUser().getUid());
    }

    @Override
    public void updaById(String out_trade_no) {
        String sql="update  tab_order set ispay='是' where out_trade_no=?";
        jdbcTemplate.update(sql,out_trade_no);
    }

    /**
     * 根据订单编号查询持久层订单对象
     * @param uid
     * @return
     */
    @Override
    public List<OrderDo> getOrderDaoByUid(String uid) {
        String sql = "select * from tab_order where uid = ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<OrderDo>(OrderDo.class),uid);
    }

    /**
     * 根据订单号查询线路id
     * @param out_trade_no
     * @return
     */
    @Override
    public int findRidByout_trade_no(String out_trade_no) {
        String sql = "select rid from tab_order where out_trade_no = ?";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class,out_trade_no);
        return integer;
    }

}
